package com.sh169.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.sh169.beans.ApplicationConfig;
import com.sh169.util.BaseHttpFilter;
/**
 *  拦截 登录页面，若有当前用户则返回到 首页中。
 * @author Administrator
 *
 */
public class LoginPageFilter extends BaseHttpFilter{
	private static Log log = Logs.getLog(LoginPageFilter.class);
	protected boolean requireLoginPage(HttpServletRequest request){
		log.debug("request uri: "+request.getRequestURI());
		log.info("login page filter: "+request.getContextPath()+ApplicationConfig.getLogin_uri());
		if(request.getRequestURI().endsWith(request.getContextPath()+ApplicationConfig.getLogin_uri())){
			return true;
		}else
			return false;
	}
	public void doFilter(HttpServletRequest request,
			HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		if(requireLoginPage(request)){
			if(request.getSession().getAttribute(SECURITY_SESSION_USER) != null){
				response.sendRedirect(request.getContextPath()+ApplicationConfig.getIndex_uri());
				return ;
			}else
				chain.doFilter(request, response);
		}else
			chain.doFilter(request, response);
	}
}
